package com.wm.yigongfang.common.config;

import com.wm.yigongfang.common.web.Result;
import com.wm.yigongfang.common.web.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author zhangPeng
 * @description
 * @date 2021/4/20 15:21
 */
@Slf4j
@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler {

  /**
   * 参数异常
   * @param e
   * @return
   */
  @ExceptionHandler(BindException.class)
  public Result<Void> handlerBindException(BindException e) {
    return new Result(ResultCode.SYSTEM_EXCEPTION.code, e.getFieldErrors().get(0).getDefaultMessage(), null);
  }


  /**
   * 业务异常
   * @param e
   * @return
   */
  @ExceptionHandler(ServiceException.class)
  public Result<Void> exceptionHandler(ServiceException e) {
    return new Result(ResultCode.SERVICE_EXCEPTION.code, e.getMessage());
  }

}
